"use strict";
/* Controllers */
app.controller("dqcSlaCheckConfigController", ['$scope', '$location', '$rootScope', '$q', '$routeParams', 'dqcSlaCheckConfigService', function ($scope, $location, $rootScope, $q, $routeParams, dqcSlaCheckConfigService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.taskType='sla_check'
    vm.p.resolveGroupId='rg-20220208102143604s6hvyy'

    vm.p.id = $routeParams.id;

    $('.date').datetimepicker({
        locale: 'zh-cn',
        format: 'HH:mm'
    });
    $('.date').on('dp.hide', function (e) {
        let curentInput = $(e.currentTarget).find('input')
        let eName = curentInput.attr('name')
        let p = $scope.vm.p
        p[eName] = curentInput.val()
        $scope.$apply()
    })

    //e_time\s*[\)]?\s*[>|>=|<|<=|=]{1,2}\s*['|"]{1}%s['|"]{1}
    var errorFn = function (result) {
        alert(result);
    };

    //    $scope.validateSql=function(){
    //        console.log("sql"+vm.p.sql)
    //    }

    $scope.save = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("保存成功！")
            } else {
                alert(response.msg);
            }
        };
        dqcSlaCheckConfigService.setDqcCheckTaskConfig(params, procFn, errorFn);
    };

    $scope.getDqcCheckTaskConfig = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data
            } else {
                alert(res.msg);
            }
        };
        return dqcSlaCheckConfigService.getDqcCheckTaskConfig(params, procFn, errorFn);
    };

    $scope.repeatFinish = function () {
        $scope.$apply();
        if ($scope.vm.p.ownerId != null && typeof ($scope.vm.p.ownerId) != 'undefined') {
            $('.selectpicker').selectpicker('val', $scope.vm.p.ownerId);
        } else {
            $scope.pingDetail().then(function () {
                $('.selectpicker').selectpicker('val', $scope.vm.p.ownerId)
            })
        }

    };

    $scope.listUser = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.listUser = res.data;
            } else {
                alert(res.msg);
            }
        };
        return dqcSlaCheckConfigService.listUser(params, procFn, errorFn);
    };

    $scope.pingDetail = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.p.ownerId = data[0].userName
        };
        return dqcSlaCheckConfigService.pingDetail(params, procFn, errorFn);
    };

    $scope.listUser()

    if (typeof (vm.p.id) != "undefined" && vm.p.id != null && vm.p.id != 0) {
        $scope.getDqcCheckTaskConfig()
    }


}]);


/* Services */
app.service('dqcSlaCheckConfigService', ['ajaxService', function (ajaxService) {

    this.setDqcCheckTaskConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dqc/setDqcCheckTaskConfig.do", successFunction, errorFunction);
    };

    this.getDqcCheckTaskConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dqc/getDqcCheckTaskConfig.do", successFunction, errorFunction);
    };

    this.listUser = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user/user_list.do", successFunction, errorFunction);
    };

    this.pingDetail = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/aaa/pingDetail.do", successFunction, errorFunction);
    };

}]);

//自定义指令实现ng-repeat完成后执行repeatFinish方法
app.directive('myRepeatFinish', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            console.log(scope.$index);
            if (scope.$last == true) {
                console.log('ng-repeat over, will draw chart.');
                scope.$eval(attr.myRepeatFinish)
            }
        }
    }
});
